using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace ComplianceFilter
{
	/// <summary>
	/// Summary description for ConfigSectionHandler.
	/// </summary>
	public class ConfigSectionHandler : IConfigurationSectionHandler
	{
		private bool _isValidDocument = true;
		private string _schemaErrors;
		/// <summary>
		/// 
		/// </summary>
		public ConfigSectionHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		/// <summary>
		/// Factory method that creates a configuration handler for a specific section of XML in the app.config.
		/// </summary>
		/// <param name="parent">Unused; was for future development (should be removed).</param>
		/// <param name="input">Unused; was for future development (should be removed).</param>
		/// <param name="section">The node.</param>
		/// <returns>UIPConfigSettings for the section.</returns>
		public object Create(object parent, object input, XmlNode section)
		{
			return Create(parent, input, section, CultureInfo.CurrentCulture);
		}

		/// <summary>
		/// Factory method that creates a configuration handler for a specific section of XML in the app.config.
		/// </summary>
		/// <param name="parent">Unused; was for future development (should be removed).</param>
		/// <param name="input">Unused; was for future development (should be removed).</param>
		/// <param name="section">The node.</param>
		/// <param name="formatProvider">The format provider.</param>
		/// <returns>UIPConfigSettings for the section.</returns>
		public object Create(object parent, object input, XmlNode section, IFormatProvider formatProvider)
		{
			ValidateSchema(section);
			return section;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="section"></param>
		private void ValidateSchema(XmlNode section)
		{
			XmlValidatingReader validatingReader = null;
			Stream xsdFile = null;
			StreamReader streamReader = null;
			try
			{
				//Validate the document using a schema
				validatingReader = new XmlValidatingReader(new XmlTextReader(new StringReader(section.OuterXml)));
				validatingReader.ValidationType = ValidationType.Schema;

				xsdFile = this.GetType().Assembly.GetManifestResourceStream("ComplianceFilter.Config.xsd");
				streamReader = new StreamReader(xsdFile);
				validatingReader.Schemas.Add(XmlSchema.Read(new XmlTextReader(streamReader), null));
				validatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

				// Validate the document
				while (validatingReader.Read())
				{
				}

				if (!_isValidDocument)
				{

                    throw new System.Configuration.ConfigurationErrorsException(string.Format("Could not read configuration section for actions : {0:s}", _schemaErrors));
				}
			}
			finally
			{
				if (validatingReader != null) validatingReader.Close();
				if (xsdFile != null) xsdFile.Close();
				if (streamReader != null) streamReader.Close();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="args"></param>
		private void ValidationCallBack(object sender, ValidationEventArgs args)
		{
			_isValidDocument = false;
			_schemaErrors += args.Message + Environment.NewLine;
		}
	}
}
